package cn.zhangxin.pethome.system.employee.service.impl;

import cn.zhangxin.pethome.base.query.BaseQuery;
import cn.zhangxin.pethome.base.service.impl.BaseServiceImpl;
import cn.zhangxin.pethome.constants.BaseConstants;
import cn.zhangxin.pethome.system.employee.domain.Employee;
import cn.zhangxin.pethome.system.employee.domain.EmployeeBo;
import cn.zhangxin.pethome.system.employee.domain.EmployeeRole;
import cn.zhangxin.pethome.system.employee.mapper.EmployeeMapper;
import cn.zhangxin.pethome.system.employee.mapper.EmployeeRoleMapper;
import cn.zhangxin.pethome.system.employee.service.IEmployeeService;
import cn.zhangxin.pethome.system.logininfo.domain.Logininfo;
import cn.zhangxin.pethome.system.logininfo.mapper.LogininfoMapper;
import cn.zhangxin.pethome.system.user.domain.User;
import cn.zhangxin.pethome.system.user.mapper.UserMapper;
import cn.zhangxin.pethome.utils.BusinessException;
import cn.zhangxin.pethome.utils.MD5Utils;
import cn.zhangxin.pethome.utils.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Employee> findAll() {
        //获得不包含数据账户和密码
        List<EmployeeBo> lists = employeeMapper.findAllBo();
        //复制
        List<Employee> employeeList = new ArrayList();
        for (EmployeeBo list : lists) {
            Employee employee = new Employee();
            //BeanUtils.copyProperties(list,employee); list:源数据 employee:目标对象
            BeanUtils.copyProperties(list,employee);
            employeeList.add(employee);
        }
        return employeeList;
    }

    @Override
    public List<Employee> findByQuery(BaseQuery baseQuery) {
        List<Employee> byQuery = employeeMapper.findByQuery(baseQuery);
        List<Employee>  employees= new ArrayList();
        for (Employee e : byQuery){
            e.setSalt(null);
            e.setPassword(null);
            Employee employee = new Employee();
            BeanUtils.copyProperties(e,employee);
            employees.add(employee);
        }
        return employees;
    }

    @Override
    public Employee findOneByLogininfoId(Long logininfoId) {
        return employeeMapper.findOneByLogininfoId(logininfoId);
    }

    @Override
    public List<String> getPermissionSn(Long employeeid) {
        return employeeMapper.getPermissionSn(employeeid);
    }

    @Override
    public List<Long> findRoleByEmpId(Long id) {
        return employeeMapper.findRoleByEmpId(id);
    }

    @Override
    public void add(Employee employee) {
        if (employee.getUsername()==null || "".equals(employee.getUsername().trim())){
            throw new BusinessException("用户名不能为空");
        }
        if (employee.getEmail()==null || "".equals(employee.getEmail().trim())){
            throw new BusinessException("用户邮箱不能为空");
        }
        if (employee.getPhone()==null || "".equals(employee.getPhone().trim())){
            throw new BusinessException("用户名手机号不能为空");
        }
        if (employee.getAge() ==null){
            throw new BusinessException("用户年龄不能为空");
        }
        if (employee.getDepartmentId()==null){
            throw new BusinessException("用户名上级部门不能为空");
        }
        if (employee.getRoleId()==null){
            throw new BusinessException("员工角色不能为空");
        }
        //校验登录表中是否有该用户 类型为管理员的 因为该结构是用于后台用户注册 所以默认就是管理员
        Logininfo logininfoByUsername = logininfoMapper.findByUsername(employee.getUsername(),BaseConstants.LogininfoType.STATE_ADMIN);
        if (logininfoByUsername!=null){
            throw new BusinessException("该用户以注册，请登录！！！");
        }
        //设置盐值和密码 使用手机号作为默认密码
        //生成32位盐值
        String salt = StrUtils.getRandomString(32);
        String password = MD5Utils.encrypByMd5(employee.getPhone() + salt);
        employee.setSalt(salt);
        employee.setPassword(password);

        //同步登录信息表
        Logininfo logininfo = new Logininfo();
        //属性几乎相同所以可以进行拷贝
        BeanUtils.copyProperties(employee,logininfo);
        //设置懂了用户的类型 通过后台添加 这里默认是管理员
        logininfo.setType(BaseConstants.LogininfoType.STATE_ADMIN);
        //设置状态默认是可用
        logininfo.setDisable(BaseConstants.LogininfoDisable.STATE_USABLE);
        //添加登录用户后 获得主键
        logininfoMapper.add(logininfo);
         employee.setLogininfoId(logininfo.getId());

         //同步user表
        User user = new User();
        BeanUtils.copyProperties(employee,user);

        //设置状态 以注册
        user.setState(BaseConstants.User.STATE_REGISTER);
        user.setCreatetime(new Date());
        userMapper.add(user);

        employeeMapper.add(employee);
        Long id = employee.getId();
        //同步员工角色表
        employeeRoleMapper.add(new EmployeeRole(id,employee.getRoleId()));
    }
}
